from pyspades.collision import distance_3d_vector
from pyspades.common import prettify_timespan
from commands import add, admin, name, get_player, alias
from twisted.internet import reactor

@name('tkcheck')
@alias('tkc')
@admin
def teamkill_check(connection, player, time = None):
    player = get_player(connection.protocol, player)
    color = connection not in connection.protocol.players and connection.colors
    minutes = float(time or 2)
    if minutes < 0.0:
        raise ValueError()
    time = reactor.seconds() - minutes * 60.0
    teammates_killed = player.teammates_killed or []
    tkmates = [b[1] for b in teammates_killed if b[0] >= time]
    player_name = player.name
    if color:
        player_name = (('\x0303' if player.team.id else '\x0302') +
            player_name + '\x0f')
    message = '%s killed %s teammate%s in the last ' % (player_name,
        len(tkmates) or 'no', '' if len(tkmates) == 1 else 's')
    if minutes == 1.0:
        message += 'minute.'
    else:
        message += '%s minutes.' % ('%f' % minutes).rstrip('0').rstrip('.')
    if len(tkmates):
        infos = set(tkmates)
        infos.discard(None)
        if color:
            names = [('\x0303' if team else '\x0302') + name for name, team in
                infos]
        else:
            names = set([name for name, team in infos])
        if len(names) > 0:
            message += (' Some of the victims were' +
                ('\x0f, ' if color else ', ').join(names))
            message += '\x0f.' if color else '.'
        else:
            message += ' Error - no name.'
        last = teammates_killed[-1]
        message += ' Last teammate was killed %s ago' % (
            prettify_timespan(reactor.seconds() - last[0], get_seconds = True))
        whom = last[1]
        if whom is not None:
            name, team = whom
            if color:
                name = ('\x0303' if team else '\x0302') + name + '\x0f'
            message += ', the teammate was %s' % name
        message += '.'
    if connection.protocol.votekick_player is player:
        dist = distance_3d_vector(player.world_object.position,
            connection.protocol.voting_player.world_object.position)
        name = ('\x0303' if connection.protocol.voting_player.team.id
            else '\x0302') + connection.protocol.voting_player.name + '\x0f'
        message += (' %s is %d tiles away from %s, who started the votekick.' %
            (player_name, int(dist), name))
    return message
add(teamkill_check)

def apply_script(protocol, connection, config):
    class TeamKillInfoConnection(connection):
        teammates_killed = None
        def on_kill(self, killer):
            if self.team == killer.team:
                if killer.teammates_killed is None:
                    killer.teammates_killed = []
                tkvictim = (self.name, self.team.id)
                info = (reactor.seconds(), tkvictim)
                killer.teammates_killed.append(info)
            return connection.on_kill(self, killer)
    return protocol, TeamKillInfoConnection
